package com.it.you.json;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.it.you.pojo.Student;
import com.it.you.pojo.Worker;
import com.sun.tools.javac.util.List;
import lombok.SneakyThrows;

import java.util.ArrayList;

public class JsonTest2 {

    @SneakyThrows
    public static void main(String[] args) {
        ArrayList<Worker> list = new ArrayList<>();
        Worker worker = new Worker();
        worker.setName("San");
        list.add(worker);
        System.out.println(JSONObject.toJSONString(list));//字段为null不输出
        System.out.println(JSON.toJSONString(list));
        System.out.println(JSONUtil.toJsonStr(list));

        System.out.println(JSON.toJSONString(list, SerializerFeature.WriteMapNullValue));//可以输出字段为null

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(list);
        System.out.println(json);
        JavaType javaType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, Student.class);
        List<String> o = objectMapper.readValue(json, javaType);
        System.out.println(o);
    }
}
